package com.tnl.utils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.wxaapp.WxaConfig;
import com.jfinal.wxaapp.api.WxaAccessToken;
import com.jfinal.wxaapp.api.WxaAccessTokenApi;
import com.tnl.base.Const;

public class WxaTemplateUtil {


    private static final String HUNTER_APPID = "wx3c02b67b861c90a0";
    private static final String HUNTER_APPSECRET = "80f8e30682b0c2b6c02f6f98609758e9";
    private static final String HUNTER_EncodingAESKey = "wobjxUL45SoGFgG4zFNJTaPhROy39mRulh2EKgzVF37";

	private static String sendApiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

    //弃用，小程序已不支持模板消息，改用订阅消息
    public static boolean sendTemplateMsg(String template) {
        WxaAccessToken accessToken = WxaAccessTokenApi.refreshAccessToken(getApiConfigZhiDao());
        String token = accessToken.getAccessToken();
        //String token = getToken(template);
        boolean flag = false;
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", token);
        JSONObject jsonResult = CommonUtil.httpsRequest(requestUrl, "POST", template);
        if (jsonResult != null) {
            int errorCode = jsonResult.getInteger("errcode");
            String errorMessage = jsonResult.getString("errmsg");
            if (errorCode == 0) {
                flag = true;
            } else {
                System.out.println("模板消息发送失败:" + errorCode + "," + errorMessage);
                flag = false;
            }
        }
        return flag;
    }

    public static ApiResult send(String jsonStr) {
        ApiConfigKit.setThreadLocalAppId(HUNTER_APPID);
        String token = AccessTokenApi.getAccessTokenStr();
        String jsonResult = HttpUtils.post(sendApiUrl + token, jsonStr);
        ApiConfigKit.removeThreadLocalAppId();
        return new ApiResult(jsonResult);
    }


    public static boolean sendTemplateYRCMsg(String template) {
        WxaAccessToken accessToken = WxaAccessTokenApi.refreshAccessToken(getApiConfigYiRenCai());
        String token = accessToken.getAccessToken();
        //String token = getToken(template);
        boolean flag = false;
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", token);
        JSONObject jsonResult = CommonUtil.httpsRequest(requestUrl, "POST", template);
        if (jsonResult != null) {
            int errorCode = jsonResult.getInteger("errcode");
            String errorMessage = jsonResult.getString("errmsg");
            if (errorCode == 0) {
                flag = true;
            } else {
                System.out.println("模板消息发送失败:" + errorCode + "," + errorMessage);
                flag = false;
            }
        }
        return flag;
    }


    /**
     * 获取token
     *
     * @param template
     * @return
     */
    public static String getToken(String template) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET".replace("APPID", Const.APPID_TIAONALI).replace("SECRET", Const.APPSECRET_TIAONALI);
        JSONObject jsonResult = CommonUtil.httpsRequest(requestUrl, "POST", template);
        if (jsonResult != null) {
            String access_token = jsonResult.getString("access_token");
            return access_token;
        } else {
            return "";
        }
    }


    /**
     * wxaconfig
     *
     * @return
     */
    public static WxaConfig getApiConfigZhiDao() {
        WxaConfig ac = new WxaConfig();

        // 配置微信 API 相关常量
        ac.setToken(Const.TOKEN);
        ac.setAppId(Const.APPID_TIAONALI);
        ac.setAppSecret(Const.APPSECRET_TIAONALI);
        ac.setEncodingAesKey(Const.EncodingAESKey_TIAONALI);
        return ac;
    }


    public static WxaConfig getApiConfigYiRenCai() {
        WxaConfig ac = new WxaConfig();

        // 配置微信 API 相关常量
        ac.setToken(Const.TOKEN);
        ac.setAppId(Const.APPID_MEDICALPERSON);
        ac.setAppSecret(Const.APPSECRET_MEDICALPERSON);
        ac.setEncodingAesKey(Const.EncodingAESKey_TIAONALI);
        return ac;
    }

}
